﻿using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace TestProject { 
    public partial class Form1 : Form     { 
        public Form1()         
        { 
            InitializeComponent();         
        }  
        private void Form1_Load(object sender, EventArgs e)         {
            //将链表写入文件 
            linktable mytable = new linktable(6);//创建链表             
            FileInfo myFile = new FileInfo("linktable.dat");             
            StreamWriter sw = myFile.CreateText();             
            string[] strs = new string[100];             
            node temp = mytable.head;             
            for (int i = 0; i < 100; i = i + 2)             
            {
                //将链表写入一个数组中 
                strs[i] = temp.num.ToString();                 
                strs[i + 1] = temp.name;                 
                if (temp == mytable.tail)                     
                    break;                 
                else 
                    temp = temp.next;             
            } 
            foreach (var s in strs)             
            { 
                sw.WriteLine(s);//写入文件             
            } 
            sw.Close();         
        }  

        private void button1_Click(object sender, EventArgs e)         
        { 
            StreamReader sr = File.OpenText("linktable.dat");             
            string nextLine; 
            if ((nextLine = sr.ReadLine()) != null)             
            { 
                int i = int.Parse(nextLine); 
                linktable table = new linktable(int.Parse(nextLine));                 
                table.head.num = int.Parse(nextLine);//写链表头num                 
                if ((nextLine = sr.ReadLine()) != null)                 
                { 
                    table.head.name = nextLine;//链表头的name                     
                    table.tail = table.head; 
                    while ((nextLine = sr.ReadLine()) != null && i > 0)                     
                    {
                        //正式写链表节点 
                        table.tail.next.num = int.Parse(nextLine)+1;//为方便测试，将读取的内容加1 
                        if ((nextLine = sr.ReadLine()) != null)                         
                        { 
                            table.tail.next.name = nextLine+"name";//同上                             
                            table.tail = table.tail.next;                             
                            i--;                         
                        }                     
                    } 
                    node temp = table.head;                     
                    for(int n = 0; n <= 6; n++)                     
                    {//测试读入的链表是否正确 
                        MessageBox.Show(temp.num.ToString());                         
                        MessageBox.Show(temp.name);                         
                        temp = temp.next;                     
                    }  
                }             
            } 
            sr.Close();         
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //序列化
            FileStream fs = new FileStream(@"E:\serializePeople.dat", FileMode.Create);
            People p = new People() { Name = "Haocheng Wu", Age = 24 };

            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(fs, p);
            fs.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //反序列化
            FileStream fs = new FileStream(@"E:\serializePeople.dat", FileMode.Open);
            BinaryFormatter bf = new BinaryFormatter();
            People p = bf.Deserialize(fs) as People;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //序列化List
            FileStream fs = new FileStream(@"E:\serializeListPeople.dat", FileMode.Create);
            BinaryFormatter bf = new BinaryFormatter();

            List<People> ps = new List<People>();
            ps.Add(new People() { Name = "EndlessRain", Age = 25 });
            ps.Add(new People() { Name = "Si", Age = 25 });

            bf.Serialize(fs, ps);
            fs.Close();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //反序列化List
            FileStream fs = new FileStream(@"E:\serializeListPeople.dat", FileMode.Open);
            BinaryFormatter bf = new BinaryFormatter();
            List<People> ps = bf.Deserialize(fs) as List<People>;

            foreach (var p in ps)
            {
                MessageBox.Show(p.Age.ToString() + ":" + p.Name.ToString());
            }
            //for (int i = 0; i < ps.Count; i++)
            //{
            //    MessageBox.Show( ps[i].Age.ToString() + ":" + ps[i].Name.ToString());
            //}
        }
    } 
    public class node 
    {
        //节点的数据结构（类）         
        public int num;         
        public string name;         
        public node next;     
    } 
    public class linktable 
    {
        //链表的数据结构（类）         
        public node head;         
        public node tail; 
        public linktable(int length)         
        { 
            head = new node();             
            head.num = 0; 
            head.name = "head";             
            tail = head; 
            for (int i = 1; i <= length; i++)             
            {
                //构造方法 
                node n = new node();                 
                n.num = i; 
                n.name = i.ToString();                 
                tail.next = n;                 
                tail = n; 
                head.num = head.num + 1;             
            }         
        }   
    } 
}
